﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Metro.AxisFromSKY.Entity;
using Metro.AxisFromSKY.Service;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Metro.AxisFromSKY
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // Add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var ConnectionStrings = Configuration.GetConnectionString("MetroConnection");

            services.AddDbContext<MetroDbContext>(
                options => options.UseSqlServer(ConnectionStrings,
                 sqlServerOptionsAction: sqlOptions =>
                 {
                     sqlOptions.EnableRetryOnFailure(
                     maxRetryCount: 10,
                     maxRetryDelay: TimeSpan.FromSeconds(30),
                     errorNumbersToAdd: null);
                 }));
            services.AddTransient<AxisService>();
            services.AddMvc().AddJsonOptions(jsonOpts =>
            {
                jsonOpts.SerializerSettings.DateFormatString = "YY-MM-DD HH:MM:SS";
                jsonOpts.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                jsonOpts.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

            });
        }

        // Configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}
